#pragma once
#include <logbase.h>
#include <systime.h>
#include <cstring.h>
#include <cycleQueHc.h>

enum PRM_INF_CFG_LEV_ENUM
{
    PRM_INF_CFG_LEV_0 = 0,
    PRM_INF_CFG_LEV_1,
    PRM_INF_CFG_LEV_2,
    PRM_INF_CFG_LEV_3,
    PRM_INF_CFG_LEV_4,
    PRM_INF_CFG_LEV_5,
    PRM_INF_CFG_LEV_6,
    PRM_INF_CFG_LEV_7,
    PRM_INF_CFG_LEV_8,// default
    PRM_INF_CFG_LEV_9,
    PRM_INF_CFG_LEV_10,
    PRM_INF_CFG_LEV_11,
    PRM_INF_CFG_LEV_12,
    PRM_INF_CFG_LEV_13,
    PRM_INF_CFG_LEV_14,
    PRM_INF_CFG_LEV_15,
};

enum PRM_INF_VAL_TYPE_ENUM
{
    PRM_INF_VAL_TYPE_32 = 0,
    PRM_INF_VAL_TYPE_64,
    PRM_INF_VAL_TYPE_FLOAT,
    PRM_INF_VAL_TYPE_DOUBLE,
    PRM_INF_VAL_TYPE_STR,
};


// #pragma packe(1)
struct prmInfBaseStc
{
    struct
    {
        uint32_t cfgLev : 8;
        uint32_t ptype : 8;
    };
    uint32_t strMaxLen : 32;
    const char *pname;// space small, if use cstring, will use mor space.
    union
    {
        int32_t *val32;
        int64_t *val64;
        float *valf;
        double *vald;
        char *valstr;
        void *valcomm;
        // struct // more 8 byte!
        // {
        //     char *valstr;
        //     uint32_t strMaxLen : 32;
        // };
    };
    union
    {
        int32_t dv32;
        int64_t dv64;
        float dvf;
        double dvd;
        const char *dvs;
    };
};

struct prmMgrBaseStc
{
    int sortflag;
    struct cycleQueRoot prmque;
    const char *prmSaveFile;
    const char *prmSaveLock;
    const char *prmSwapFile;
    pthread_mutex_t prmMgrLock;
    int prmFileLockFd;
};

#define PRM_STR_VAL_MAX_LEN (2048) // param value max len <= 2048
#define PRM_STR_NAME_MAX_LEN (128) // param name len < 128.

int prmMgrInit(struct prmMgrBaseStc *pMgrPtr, const char *cfgSavef , const char *cfgSwapf, const char *cfgFileLock);
int prmMgrFree(struct prmMgrBaseStc *pMgrPtr);
int prmMgrRegI32(struct prmMgrBaseStc *pMgrPtr, const char *setname, int32_t *setValPtr, int32_t defval, uint32_t setlev);
int prmMgrRegI64(struct prmMgrBaseStc *pMgrPtr, const char *setname, int64_t *setValPtr, int64_t defval, uint32_t setlev);
int prmMgrRegFloat(struct prmMgrBaseStc *pMgrPtr, const char *setname, float *setValPtr, float defval, uint32_t setlev);
int prmMgrRegDouble(struct prmMgrBaseStc *pMgrPtr, const char *setname, double *setValPtr, double defval, uint32_t setlev);
int prmMgrRegStr(struct prmMgrBaseStc *pMgrPtr, const char *setname, char *setValPtr, uint32_t setMaxSize, const char *defval, uint32_t setlev);
int prmMgrRegFinish(struct prmMgrBaseStc *pMgrPtr);
int prmMgrReset(struct prmMgrBaseStc *pMgrPtr, uint32_t optlev);
int prmMgrSetStrVal(struct prmMgrBaseStc *pMgrPtr, const char *setname, const char *setStrVal, uint32_t optlev);
int prmMgrGetStrVal(struct prmMgrBaseStc *pMgrPtr, const char *optname, char *optStrVal, uint32_t strMaxLen, uint32_t optlev);
int prmMgrPrintVals(struct prmMgrBaseStc *pMgrPtr);

int prmMgrReadFile(struct prmMgrBaseStc *pMgrPtr);
int prmMgrSaveFile(struct prmMgrBaseStc *pMgrPtr);
